﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
using VOL;

namespace Shopper
{
    public partial class frmDMKhachManager : DevExpress.XtraEditors.XtraForm
    {
        DMKhachBL khachBL = new DMKhachBL();
        public frmDMKhachManager()
        {
            InitializeComponent();
        }

        private void frmDMKhachManager_Load(object sender, EventArgs e)
        {
            FillData();
        }

        private void FillData()
        {
            DataTable dt = khachBL.GetData();
            gridControl1.DataSource = dt;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            DMKhach objKhach = new DMKhach();

            using (frmDMKhachForm frm = new frmDMKhachForm())
            {
                if (frm.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    objKhach.HoTen = frm.HoTen;
                    objKhach.DiaChi = frm.DiaChi;
                    objKhach.DienThoai = frm.DienThoai;
                    objKhach.Email = frm.Email;

                    khachBL.insertDMKhach(objKhach);
                    XtraMessageBox.Show("Đã thêm người dùng thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FillData();
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            using (frmDMKhachForm frm = new frmDMKhachForm())
            {
                int MaKhach = (int)gridView1.GetFocusedRowCellValue("MaKhach");
                DMKhach objKhach = new DMKhach();
                objKhach.MaKhach = MaKhach;

                DataTable dt = khachBL.searchIDDMKhach(objKhach);
                if (dt.Rows.Count > 0)
                {
                    frm.HoTen = dt.Rows[0]["HoTen"].ToString();
                    frm.DiaChi = dt.Rows[0]["DiaChi"].ToString();
                    frm.DienThoai = dt.Rows[0]["DienThoai"].ToString();
                    frm.Email = dt.Rows[0]["Email"].ToString();
                }

                if (frm.ShowDialog(this.FindForm()) == System.Windows.Forms.DialogResult.OK)
                {
                    DMKhach objKhachNew = new DMKhach();
                    objKhachNew.HoTen = frm.HoTen;
                    objKhachNew.DiaChi = frm.DiaChi;
                    objKhachNew.DienThoai = frm.DienThoai;
                    objKhachNew.Email = frm.Email;
                    objKhach.MaKhach = MaKhach;

                    khachBL.updateDMKhach(objKhachNew);

                    XtraMessageBox.Show("Đã thay đổi thông tin khách hàng thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FillData();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult dr = XtraMessageBox.Show("Bạn có muốn xóa bản ghi này hay k?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == System.Windows.Forms.DialogResult.Yes)
            {
                var row = gridView1.FocusedRowHandle;
                var obj = gridView1.GetRowCellValue(row, "MaKhach");

                DMKhach objKhach = new DMKhach();
                objKhach.MaKhach = (int)obj;
                khachBL.deleteDMKhach(objKhach);
                FillData();
            }
        }
    }
}